using Senparc.Weixin.Entities;
using Senparc.Weixin.HttpUtility;
using Senparc.Weixin.Open.CommonAPIs;
using System;
namespace Senparc.Weixin.Open.ComponentAPIs
{
	public static class ComponentApi
	{
		public static string GetComponentLoginPageUrl(string componentAppId, string preAuthCode, string redirectUrl)
		{
			return string.Format("https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid={0}&pre_auth_code={1}&redirect_uri={2}", componentAppId, preAuthCode, redirectUrl.UrlEncode());
		}
		public static QueryAuthResult QueryAuth(string componentAccessToken, string componentAppId, string authorizationCode, int timeOut = 10000)
		{
			string urlFormat = string.Format("https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token={0}", componentAccessToken);
			var data = new
			{
				component_appid = componentAppId,
				authorization_code = authorizationCode
			};
			return CommonJsonSend.Send<QueryAuthResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static RefreshAuthorizerTokenResult RefreshAuthorizerToken(string componentAccessToken, string componentAppId, string authorizerAppId, string authorizerRefreshToken, int timeOut = 10000)
		{
			string urlFormat = string.Format("https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token={0}", componentAccessToken);
			var data = new
			{
				component_appid = componentAppId,
				authorizer_appid = authorizerAppId,
				authorizer_refresh_token = authorizerRefreshToken
			};
			return CommonJsonSend.Send<RefreshAuthorizerTokenResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static GetAuthorizerInfoResult GetAuthorizerInfo(string componentAccessToken, string componentAppId, string authorizerAppId, int timeOut = 10000)
		{
			string urlFormat = string.Format("https://api.weixin.qq.com/cgi-bin/component/api_authorizer_info?component_access_token={0}", componentAccessToken);
			var data = new
			{
				component_appid = componentAppId,
				authorizer_appid = authorizerAppId
			};
			return CommonJsonSend.Send<GetAuthorizerInfoResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static AuthorizerOptionResult GetAuthorizerOption(string componentAccessToken, string componentAppId, string authorizerAppId, OptionName optionName, int timeOut = 10000)
		{
			string urlFormat = string.Format("https://api.weixin.qq.com/cgi-bin/component/api_authorizer_option?component_access_token={0}", componentAccessToken);
			var data = new
			{
				component_appid = componentAppId,
				authorizer_appid = authorizerAppId,
				option_name = optionName
			};
			return CommonJsonSend.Send<AuthorizerOptionResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static WxJsonResult SetAuthorizerOption(string componentAccessToken, string componentAppId, string authorizerAppId, OptionName optionName, int optionValue, int timeOut = 10000)
		{
			string urlFormat = string.Format("https://api.weixin.qq.com/cgi-bin/component/api_authorizer_option?component_access_token={0}", componentAccessToken);
			var data = new
			{
				component_appid = componentAppId,
				authorizer_appid = authorizerAppId,
				option_name = optionName,
				option_value = optionValue
			};
			return CommonJsonSend.Send<WxJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
	}
}
